
/**
* 上海中赢金融信息服务有限公司
* Copyright (c) 2017-2027 Chinazyjr,Inc.All Rights Reserved.
*/
package com.sys.midware.redis.core.client;

import java.util.HashSet;
import java.util.Set;

import com.sys.midware.logger.Logger;
import com.sys.midware.logger.LoggerFactory;
import com.sys.midware.redis.config.RedisConfig;
import com.sys.midware.redis.config.RedisConfigHolder;
import com.sys.midware.redis.config.RedisConfigUtil;
import com.sys.midware.redis.config.RedisNode;
import com.sys.midware.redis.config.RedisPoolConfig;
import com.sys.midware.redis.core.RedisFactory;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

/**
 * <b>Description：</b> TODO <br/>
 * <b>ClassName：</b> Sigle <br/>
 * <b>@author：</b> jackyshang <br/>
 * <b>@date：</b> 2016年8月19日 上午10:49:34 <br/>
 * <b>@version: </b> <br/>
 */
public class RedisClusterClient  {
    private static final Logger logger = LoggerFactory.getLogger(RedisFactory.class);

    private RedisClusterClient(){};
    
    private static RedisClusterClient instance;
    /**
     * 获取单例工厂
     * 
     * @return
     * @throws Exception
     */
    public static RedisClusterClient getInstance() {
        if (instance != null)
            return instance;

        synchronized (RedisClusterClient.class) {
            if (instance == null) {
                instance = new RedisClusterClient();
                initPool();
            }

            return instance;
        }
    }
    /**
     * 单节点的jedisPool
     */
    private static JedisCluster clusterClient = null;

    public static final int DEFAULT_TIMEOUT = 2000;

    
    /**
     * 初始化Redis连接池
     */
    private static synchronized void initPool() {
        if (clusterClient != null) {
            return;
        }
        RedisConfigUtil.initConfig();
        RedisConfig redisConfig = RedisConfigHolder.get();
        RedisPoolConfig poolConfig = redisConfig.getRedisPoolConfig();
        
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(poolConfig.getMaxIdle());
        config.setMaxTotal(poolConfig.getMaxTotal());
        config.setMaxWaitMillis(poolConfig.getMaxWait());
        config.setMinEvictableIdleTimeMillis(poolConfig.getMinEvictableIdleTimeMillis());
        config.setMinIdle(poolConfig.getMinIdle());
        config.setTestOnBorrow(poolConfig.isTestOnBorrow());
        config.setTestOnReturn(poolConfig.isTestOnReturn());
        Set<HostAndPort>haList = new HashSet<HostAndPort>();
        // 初始化jedis,
        for (RedisNode redisNode : redisConfig.getNodeList()) {
            haList.add(new HostAndPort(redisNode.getHost(),redisNode.getPort()));
        }
        clusterClient = new JedisCluster(haList,2000,100, config);

    }

    /**
     * 同步获取Jedis实例
     * 
     * @return Jedis
     */
    public  synchronized JedisCluster getClient() {
  
        return clusterClient;
    }
    
//    /**
//     * 释放jedis资源
//     * 
//     * @param jedis
//     */
//    public  void close(final Jedis jedis) {
//        if (jedis != null) {
//            jedis.close();
//        }
//    }

}
